<?php

// +----------------------------------------------------------------------
// | 分类控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\app\admin;

use app\common\event\SystemConfig as SystemConfigEvent;
use app\common\model\SystemConfig as SystemConfigModel;
use app\model\app\ExchangeCoupon as ExchangeCouponModel;
use yicmf\builder\Edit;
use yicmf\builder\Table;
use think\facade\Url;
use app\controller\admin\Common;
use app\model\app\Dynamic as DynamicModel;
use app\model\app\Version as VersionModel;


class Version extends Common
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function _filter()
    {
    }

    public function index()
    {
        //读取规则列表
        $map = $this->_search($this->dbname);
        //            //显示页面
        $builder = new Table();
        $builder->model(VersionModel::class)
            ->where($map)
            ->buttonUpdate('setting', '修改配置')
            ->order('id DESC')
            ->setSearchPostUrl('', '')
            ->buttonUpdate('update', '新增版本')
            ->keyId()
            ->keyText('title', '标题')
            ->keyText('describe', '更新内容')
            ->keyText('version_name', '版本名')
            ->keyText('version_code', '版本号', '')
            ->keyBool('is_silence', '静默更新')
            ->keyBool('is_examine', '是否审核')
            ->keyBool('is_force', '强制更新')
            ->actionUpdate();


        return $builder->fetch();
    }
    public function setting()
    {
        if ($this->request->isPost()) {
            $event = new SystemConfigEvent();
            $re = $event->updateValue($this->request->post(), $this->user);
            $this->_returnAuto($re, false);
        } else {
            $set = SystemConfigModel::where('status', 1)->where('group', 'app_store')->where('is_dev', 0)->order('sort ASC')->select();
            return SystemConfigEvent::builderView($set, '应用商店配置');
        }
    }

    /**
     * 编辑分类
     * @access protected
     * @author 微尘 <yicmf@qq.com>
     */
    public function update()
    {
        if ($this->request->isPost()) { //提交表单
            if ($this->request->param('id')) {

                $data = VersionModel::where('id', $this->request->param('id'))->find();
                $info = $this->request->only(['title','is_test', 'url', 'image_id', 'summary', 'sort', 'is_open','need_version_code','is_examine']);
                $data->save($info);
                $this->_returnSuccess('更新成功');
            } else {
                VersionModel::create($this->request->param());
                $this->_returnSuccess('新增成功');
            }
        } else {
            /* 获取分类信息 */
            $id = $this->request->get('id');
            $isEdit = $id ? true : false;
            if ($isEdit) {
                $data = VersionModel::where('id', $id)->find();
                if (!$data) {
                    $this->_returnError('分类不存在或者已经删除');
                }
            } else {
                $data['style'] = 3;
                $data['is_force'] = 1;
                $data['appid'] = '__UNI__A9CDDC7';
                $data['platform'] = 'android';
            }

            $builder = new Edit();
            $builder->title('版本管理')
                ->keyHidden('appid')
                ->keyHidden('platform')
                ->keyHidden('is_force')
                ->keyBool('is_test', '测试版','',0)
                ->keyBool('is_silence', '静默更新','',1)
                ->keyText('title', '更新提示标题', '', '', '')
                ->keyEditor('describe', '更新内容', '')
                ->keyTextInline('version_name', '版本名', '')
                ->keyTextInline('version_code', '版本号', '')
                ->keyTextInline('need_version_code', '最低版本号', '默认不要求')
                ->keyAttachmentModel('wgt_url_id', '静默更新文件')
                ->keyAttachmentModel('pkg_url_id', '整包更新文件')
                ->keyBool('is_examine', '审核版','打开审核版，版本不更新')
                ->keyBool('is_force', '强制更新')
                ->data($data)
                ->setTrigger('is_silence', 1, 'wgt_url_id')
                ->setTrigger('is_silence', 0, 'pkg_url_id,title,describe,is_force,is_examine')
                ->buttonSubmit('', '保存');
            //                    ->buttonClose();
            return $builder->fetch();
        }
    }
}